using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
using System.IO;

namespace OMR2 {
	internal class Sample {
		// Fields
		private string ConnectionString;
		private string OutputPath;
		private string Prefix;
		private string Query;
		public string SQL;
		private string Subkeyname;
		private int Timeout;
		// Methods
		public Sample(string subkeyname,string prefix,string query,string conStr,string outputPath,int timeout) {
			this.Prefix=prefix;
			this.Query=query;
			this.ConnectionString=conStr;
			this.OutputPath=outputPath;
			this.Subkeyname=subkeyname;
			this.Timeout=timeout;
		}
		internal string Log() {
			object obj2;
			string str="";
			DateTime now=DateTime.Now;
			Stopwatch stopwatch=Stopwatch.StartNew();
			try
			{
				str=string.Concat(new object[] { this.Subkeyname,'.',this.Prefix,':' });
				using(DataTable table=this.RunSQL(this.Query))
				{
					obj2=str;
					str=string.Concat(new object[] { obj2,table.Rows.Count," Rows, ",(int)stopwatch.ElapsedMilliseconds," msec." });
					string msg=this.MakeCsv(table)+((int)stopwatch.ElapsedMilliseconds);
					this.LogToFile(now,msg);
					this.SQL=this.LogToDb(table,this.Prefix,(int)stopwatch.ElapsedMilliseconds);
				}
			}
			catch(SqlException exception)
			{
				EventLog.WriteEntry("OMRTimeout",this.Subkeyname+"."+this.Prefix+Environment.NewLine+exception.ToString(),EventLogEntryType.Error);
				obj2=str;
				str=string.Concat(new object[] { obj2," Timeout! ",(int)stopwatch.ElapsedMilliseconds," msec." });
			}
			catch(Exception exception2)
			{
				EventLog.WriteEntry("OMR",this.Subkeyname+"."+this.Prefix+Environment.NewLine+exception2.ToString(),EventLogEntryType.Error);
				obj2=str;
				str=string.Concat(new object[] { obj2," Exception! ",(int)stopwatch.ElapsedMilliseconds," msec." });
			}
			return str;
		}
		private string LogToDb(DataTable tab,string statName,int statTime) {
			StringBuilder builder=new StringBuilder();
			builder.AppendFormat("insert into OmrChk(name,stat,cnt)values('{0}',{1},{2})",statName,statTime,tab.Rows.Count);
			builder.AppendLine();
			if(tab.Rows.Count>0)
			{
				builder.AppendLine("select @chkid=scope_identity()");
				foreach(DataRow row in tab.Rows)
				{
					object obj2=row[0];
					if(obj2==DBNull.Value)
					{
						obj2="<NULL>";
					}
					if(obj2.ToString().Trim()=="")
					{
						obj2="<EMPTY>";
					}
					builder.AppendFormat("insert into OmrData(chkid,qkey,qval)values(@chkid,'{0}',{1})",obj2,row[1]);
					builder.AppendLine();
				}
			}
			return builder.ToString();
		}
		private void LogToFile(DateTime time,string msg) {
			File.AppendAllText(this.OutputPath+@"\"+this.Prefix+time.ToString("yy-MM-dd")+".log",time.ToString("HH:mm:ss;")+msg+Environment.NewLine);
		}
		private string MakeCsv(DataTable tab) {
			StringBuilder builder=new StringBuilder();
			foreach(DataRow row in tab.Rows)
			{
				object obj2=row[0];
				if(obj2==DBNull.Value)
				{
					obj2="<NULL>";
				}
				if(obj2.ToString().Trim()=="")
				{
					obj2="<EMPTY>";
				}
				builder.AppendFormat("{0}={1};",obj2,row[1]);
			}
			return builder.ToString();
		}
		private DataTable RunSQL(string sql) {
			DataTable dataTable=new DataTable();
			using(SqlDataAdapter adapter=new SqlDataAdapter(sql,this.ConnectionString))
			{
				adapter.SelectCommand.CommandTimeout=this.Timeout;
				adapter.Fill(dataTable);
			}
			return dataTable;
		}
		public override string ToString() {
			return this.Prefix;
		}
	}
}
